# coding:utf-8

import time
import openai
from openai.error import RateLimitError, Timeout, APIError, APIConnectionError

openai.api_base = "https://api.openai-proxy.com/v1"
openai.api_key = "sk-proj-SklOX1cUH8JApZZKXyLQT3BlbkFJUVo2M6WdJekiYF3DxmQZ"

delay_time = 0.5
decay_rate = 0.8


def get_responses_GPT(prompts: str, histories, model="gpt-3.5-turbo-0125", max_tokens=1024, temperature=0.8,
                      system_message=None, logprobs=None, echo=False):
    global delay_time, cur_key_idx
    # Send request
    # try:
    results = []

    response = openai.ChatCompletion.create(
        model=model,
        messages=[
            {
                "role": "system",
                "content": system_message or "你是一名图书管理员。"
            },
            *histories,
            {
                "role": "user",
                "content": prompts
            }
        ],
        temperature=temperature,
        max_tokens=max_tokens,
        top_p=1,
        frequency_penalty=0.0,
        presence_penalty=0.0,
        request_timeout=60
    )
    results.append(response["choices"][0])
    return results[0]["message"]["content"]


